package mya.mes.mdc.logic.utils;

import java.time.Duration;
import java.time.LocalDateTime;

public class TimeUtils {

    public static long timeSpan(String startDate, String startTime, String endDate, String endTime) {
        LocalDateTime start = LocalDateTime.parse(startDate + "T" + startTime);
        LocalDateTime end = LocalDateTime.parse(endDate + "T" + endTime);

        Duration duration = Duration.between(start, end);

        return duration.getSeconds();
    }


    public static long timeSpan(long startTime, long endTime) {
        if (startTime > endTime) {
            return startTime - endTime;
        }


        return endTime - startTime;
    }


    /**
     * 转换为秒作为单位
     *
     * @param timeSpan
     * @return
     */
    public static long asSeconds(long timeSpan) {
        long tmp = timeSpan % 1000;

        long seconds = (timeSpan - tmp) / 1000;

        if (tmp > 500) {
            seconds += 1;
        }

        return seconds;
    }

    public static String formatTimeSpan(long timeSpan) {
        long tmp = timeSpan;

        int hour = 0;
        int minute = 0;
        long second = 0;

        if (tmp > 60 * 60) {

            tmp = timeSpan % 3600;

            hour = (int) ((timeSpan - tmp) / 3600L);

        }

        if (tmp > 60) {
            long tmp1 = tmp % 60;

            minute = (int) ((tmp - tmp1) / 60);

            tmp = tmp1;

        }

        second = tmp;


        String str = String.format("%d小时%d分%d秒", hour, minute, second);

        return str;

    }
}
